<?php 
/* this file will handle user shipping details and send email order for processing */
// load wordpress defaults, make global variables/functions available
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];
require( $this_file . 'wp-blog-header.php' );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>

<!-- head start -->

<head profile="http://gmpg.org/xfn/11">
	<?php
    $aOptions = SofaSuppaStore::initOptions( false );
	$entrycurrency = $aOptions[ 'entrycurrency' ];
	// handle font selection
	$google_fonts = $aOptions[ 'google_fonts' ];
	$font_str = $google_fonts;
	$font_str_arr = explode( ':', $font_str );
	$font_name = $font_str_arr[ 0 ];
	$clean_font_arr = explode( '+', $font_name );
	$clean_font_name = implode( ' ', $clean_font_arr );
    ?>
    <link href='http://fonts.googleapis.com/css?family=<?php echo $google_fonts; ?>' rel='stylesheet' type='text/css' />
    <link rel="stylesheet" href="<?php echo( get_template_directory_uri() . '/960.css' ); ?>" type="text/css" media="screen, projection" />
    <link rel="stylesheet" href="<?php echo( get_template_directory_uri() . '/screen.css' ); ?>" type="text/css" media="screen, projection" />
    <style type="text/css">
	/* handle basic styles according to google fonts selection */
	h1, h2, h3, h4, h5, h6 { font-family: '<?php echo $clean_font_name; ?>', Arial, sans-serif; font-weight: normal; }
	</style>
	<link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>" type="text/css" media="screen, projection" />
    
    <?php wp_print_scripts( 'jquery' ); ?>
</head>

<!-- head end -->

<body>

<!-- main content start -->

<div id="lightcontent" class="container_8 clearfix">
    
	<?php
	// get error details
	if( $_SERVER[ 'REQUEST_METHOD' ] == 'GET' ) {
		$mo = !empty( $_GET[ 'mo' ] ) ? stripslashes( strip_tags( $_GET[ 'mo' ] ) ) : NULL; // id
		$loc = !empty( $_GET[ 'loc' ] ) ? stripslashes( strip_tags( urldecode( $_GET[ 'loc' ] ) ) ) : NULL; // ship to
		$pri = !empty( $_GET[ 'pri' ] ) ? floatval( $_GET[ 'pri' ] ) : -1; // ship price
		$dis = !empty( $_GET[ 'dis' ] ) ? floatval( $_GET[ 'dis' ] ) : -1; // discount price
	} elseif( $_SERVER[ 'REQUEST_METHOD' ] == 'REQUEST' ) {
		$mo = !empty( $_REQUEST[ 'mo' ] ) ? stripslashes( strip_tags( $_REQUEST[ 'mo' ] ) ) : NULL; // id
		$loc = !empty( $_REQUEST[ 'loc' ] ) ? stripslashes( strip_tags( urldecode( $_REQUEST[ 'loc' ] ) ) ) : NULL; // ship to
		$pri = !empty( $_REQUEST[ 'pri' ] ) ? floatval( $_REQUEST[ 'pri' ] ) : -1; // ship price
		$dis = !empty( $_REQUEST[ 'dis' ] ) ? floatval( $_REQUEST[ 'dis' ] ) : -1; // discount price
	}
	?>
    
	<?php if( $mo == 'sofasuppastore' && $loc > -1 && $pri > -1 ) { ?>
    
    <div id="featured" class="castshadow grid_8">
    
    	<!-- title and intro text start -->
    
        <div class="grid_8 alpha omega">
        	<h2 class="grid_8 alpha omega"><?php _e( "Shipping Details", "sofa_suppastore" ); ?></h2>
            <p><?php _e( "Please be sure to enter all required information, otherwise your order will not be accepted. Mandatory fields are marked with asterisk (*).", "sofa_suppastore" ); ?></p>
        </div>
        
        <!-- title and intro text end -->
        
        <!-- try again start -->
        
        <div id="submitmessenger" class="grid_8 alpha omega">
        	<span class="clear">&nbsp;</span>
            <span id="redosubmit"><?php _e( "OK, I'll try again!", "sofa_suppastore" ); ?></span>
        </div>
        
        <!-- try again end -->
        
        <!-- shipping details form start -->
        
        <form id="ppform" name="ppform" method="post" action="<?php echo get_template_directory_uri() . ( '/gateways/mail_order_handler.php' ); ?>">
        
        <div class="grid_4 alpha">
            <p>
                <label for="ab_firstname">* <?php _e( "First Name", "sofa_suppastore" ); ?></label>
                <input type="text" size="39" id="ab_firstname" name="ab_firstname" value="" tabindex="333" />
            </p>
            <p>
                <label for="ab_lastname">* <?php _e( "Last Name", "sofa_suppastore" ); ?></label>
                <input type="text" size="39" id="ab_lastname" name="ab_lastname" value="" tabindex="334" />
            </p>
            <p>
                <label for="ab_cellphone"><?php _e( "Cell Phone", "sofa_suppastore" ); ?></label>
                <input type="text" size="39" id="ab_cellphone" name="ab_cellphone" value="" tabindex="335" />
            </p>
            <p>
                <label for="ab_phone"><?php _e( "Phone Number", "sofa_suppastore" ); ?></label>
                <input type="text" size="39" id="ab_phone" name="ab_phone" value="" tabindex="336" />
            </p>
            <p>
                <label for="ab_email">* <?php _e( "E-mail Address", "sofa_suppastore" ); ?></label>
                <input type="text" size="39" id="ab_email" name="ab_email" value="" tabindex="337" />
            </p>
        </div>
        
        <div class="grid_4 omega">
            <p>
                <label for="ab_address">* <?php _e( "Shipping Address", "sofa_suppastore" ); ?></label>
                <input type="text" size="39" id="ab_address" name="ab_address" value="" tabindex="338" />
            </p>
            <p>
                <label for="ab_address2"><?php _e( "Shipping Address 2", "sofa_suppastore" ); ?></label>
                <input type="text" size="39" id="ab_address2" name="ab_address2" value="" tabindex="339" />
            </p>
            <p>
                <label for="ab_city">* <?php _e( "City", "sofa_suppastore" ); ?></label>
                <input type="text" size="39" id="ab_city" name="ab_city" value="" tabindex="340" />
            </p>
            <p>
                <label for="ab_zipcode">* <?php _e( "ZIP Code", "sofa_suppastore" ); ?></label>
                <input type="text" size="39" id="ab_zipcode" name="ab_zipcode" value="" tabindex="341" />
            </p>
            <p>
                <label for="ab_country_quasi"><?php _e( "Selected Country (can't be changed)", "sofa_suppastore" ); ?></label>
                <input type="text" size="39" disabled="disabled" id="ab_country_quasi" name="ab_country_quasi" value="<?php echo $loc . ', ' . $entrycurrency . ' ' . number_format( $pri, 2, '.', '' ); ?>" />
            </p>
        </div>
        
        <input type="hidden" size="39" id="ab_discount" name="ab_discount" value="<?php echo $dis; ?>" />
        <input type="hidden" size="39" id="ab_country" name="ab_country" value="<?php echo $loc . '~' . $pri; ?>" />
        <?php wp_nonce_field( 'chck_ref_cc', 'sofa_suppastore_cc' ); ?>
        
        </form>
        
        <!-- shipping details form end -->
        
        <!-- out of form button start -->
        
        <div id="buttholder" class="center grid_8 alpha omega">
                <button id="pyo" name="pyo" tabindex="449"><?php _e( "Place Your Order!", "sofa_suppastore" ); ?></button>
        </div>
        
        <!-- out of form button end -->
        
		<script type='text/javascript'>
        
            jQuery( document ).ready( function() {
				
				// check if mandatory information gets filled properly
				jQuery( '#pyo' ).click( function() {
					
					var backup_messenger = jQuery( '#submitmessenger' ).html();
					var message = '';
					var first_name = jQuery( '#ab_firstname' ).val();
					var last_name = jQuery( '#ab_lastname' ).val();
					var email_address = jQuery( '#ab_email' ).val();
					var shipping_address = jQuery( '#ab_address' ).val();
					var city = jQuery( '#ab_city' ).val();
					var zipcode = jQuery( '#ab_zipcode' ).val();
					
					if( first_name.length < 2 ) {
						message += ( "<p>&raquo; " + "<?php _e( "First Name field is mandatory and may not contain less than 2 characters.", "sofa_suppastore" ); ?>" + "</p>" );
					}
					if( last_name.length < 2 ) {
						message += ( "<p>&raquo; " + "<?php _e( "Last Name field is mandatory and may not contain less than 2 characters.", "sofa_suppastore" ); ?>" + "</p>" );
					}
					if( !validateEmail( email_address ) ) {
						message += ( "<p>&raquo; " + "<?php _e( "E-mail Address field is mandatory.", "sofa_suppastore" ); ?>" + "</p>" );
					}
					if( shipping_address.length < 5 ) {
						message += ( "<p>&raquo; " + "<?php _e( "Shipping Address field is mandatory and may not contain less than 10 characters.", "sofa_suppastore" ); ?>" + "</p>" );
					}
					if( city.length < 2 ) {
						message += ( "<p>&raquo; " + "<?php _e( "City field is mandatory and may not contain less than 2 characters.", "sofa_suppastore" ); ?>" + "</p>" );
					}
					if( zipcode.length < 4 ) {
						message += ( "<p>&raquo; " + "<?php _e( "ZIP Code field is mandatory and may not contain less than 5 characters.", "sofa_suppastore" ); ?>" + "</p>" );
					}

					if( message == '' ) jQuery( '#ppform' ).submit();
					else {
						jQuery( '#buttholder' ).hide( 'fast' );
						jQuery( '#ppform' ).hide( 'slow' );
						jQuery( '#submitmessenger' ).show( 'slow' );
						jQuery( '#submitmessenger' ).html( '<h3><?php _e( "Error!", "sofa_suppastore" ); ?><\/h3>' + message + backup_messenger );
						
						jQuery( '#redosubmit' ).click( function() {
							jQuery( '#submitmessenger' ).hide( 'slow', function() { jQuery( '#submitmessenger' ).html( backup_messenger ); } );
							jQuery( '#ppform' ).show( 'slow', function() { jQuery( '#buttholder' ).show( 'fast' ); } );
							return false;
							
						} );
					}
					
					return false;
					
				} );
				
            } );
			
			function validateEmail( email ) {  
				return email.match( /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/ );
			}
        
    	</script>
        
    </div>
    
    <?php } else { ?>
    
    <!-- permission denied start -->
    
    <div id="featured" class="grid_8">
    
        <h2 class="grid_8 alpha omega"><?php _e( "Error!", "sofa_suppastore" ); ?></h2>
        
        <div id="proddetails" class="grid_8 alpha omega">
        
        <p><?php _e( "You are not allowed to perform this action.", "sofa_suppastore" ); ?></p>
        
        </div>
        
    </div>
    
    <!-- permission denied end -->
    
    <?php } ?>
        
</div>

<!-- main content end -->

</body>
</html>